package com.dotools.utils;

import android.app.Activity;
import android.app.Notification;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DevicesUtils_Oppo {
    private static Boolean isVivoDevice = null;

    public static Boolean isOppoDevice() {
        if (isVivoDevice == null) {
            isVivoDevice = Build.BRAND.equalsIgnoreCase("OPPO");
        }
        return isVivoDevice;
    }


    public static String getOPPORomVersionCode() {
        String version = getRom();
        if (version != null) {
            return version.split("[.]")[0];
        } else {
            return null;
        }
    }


    /**
     * 打开该应用在oppo的权限管理页面
     *
     * @param context
     * @return
     */
    public static boolean openAutoOPPOSetting(Context context) {
        Intent intent = new Intent("com.oppo.safe.permission.PermissionTopActivity");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(ComponentName.unflattenFromString("com.coloros.safecenter/.permission.PermissionTopActivity"));
        boolean isok = false;
        try {
            context.startActivity(intent);
            isok = true;
        } catch (Exception e) {
            try {
                Intent intent2 = new Intent("com.oppo.safe.permission.PermissionTopActivity");
                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent2.setComponent(ComponentName.unflattenFromString("com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity"));
                context.startActivity(intent2);
                isok = true;
            } catch (Exception e2) {
                try {
                    Intent intent3 = new Intent();
                    intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent3.setComponent(ComponentName.unflattenFromString("com.color.safecenter/.permission.PermissionManagerActivity"));
                    context.startActivity(intent3);
                    isok = true;
                } catch (Exception e3) {
                    isok = false;
                }
            }
        }
        return isok;
    }

    /**
     * 打开oppo的通知栏管理页面
     * @param context
     * @return
     */
    public static boolean openNotificationSetting(Context context) {
        Intent intent = new Intent();
        try {
            //直接跳转到应用通知设置的代码：
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                intent.putExtra(Notification.EXTRA_CHANNEL_ID, context.getApplicationInfo().uid);
                context.startActivity(intent);
            }else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
                context.startActivity(intent);
            }else  if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(intent);
            }
            return true;
        } catch (Exception e) {
            intent.setAction(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
            e.printStackTrace();
            return false;
        }
    }

    /****
     * 跳转OPPO市场详细APP页面
     * @param act   必须是ACTIVITY
     * @param pkg  APP包名
     */
    public static void jump2OppoMarket(Activity act, String pkg) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + pkg));
        act.startActivity(intent);
    }


    private static String getRom() {
        return getSystemProperty("ro.build.version.opporom");
    }

    private static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
        return line;
    }
}
